/**
 *  @file Themes.java
 *  @author Mamontov
 *
 *  This file contains Themes class definition
 */
package db21.entities;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.rdf.model.Property;
import db21.racerpro.Queries;
import java.util.ArrayList;

/**
 * @class  Themes
 * @author Mamontov
 *
 *
 */
public class Themes extends Entities {
    public Themes() {
        m_conceptname = "Theme";
    }
    
    /**
     * You should use only this function to create new resource, because only
     * it cares about necessary assertion
     * @param resource
     * @return whether it was successfully created
     */
    public boolean add(Theme theme) {
        Individual ind = this.makeAndReturnIndividual(m_conceptname, theme.name);
        if (ind == null) return false;
        Property prop;
        Individual right;
        if (theme.hasParentTheme) {
            prop = m_conn.getProperty(Queries.includedInto());
            right = m_conn.getIndividual(theme.parentTheme);
            ind.addProperty(prop, right);
        }
        if (theme.subject != null) {
            prop = m_conn.getProperty(Queries.relatesTo());
            right = m_conn.getIndividual(theme.subject);
            ind.addProperty(prop, right);
        }
        // Assert competences
        prop = m_conn.getProperty(Queries.educates());
        for(int i = 0; i < theme.competences.size(); i++) {
            Individual competence = m_conn.getIndividual(theme.competences.get(i));
            ind.addProperty(prop, competence);
        }
        m_conn.syncWithRacerPro();
        return true;
    }
    
    public ArrayList<String> listSubthemes(String iri) {
        String escaped = Queries.escapeValue(m_conn.prefix(), iri);
        String query  = m_conn.buildQuery(Queries.listSubthemes(), escaped);
        return this.fetchAsRow(query);
    }
    
    public ArrayList<String> listRelatedToSubject(String subject) {
        String escaped = Queries.escapeValue(m_conn.prefix(), subject);
        String query  = m_conn.buildQuery(Queries.listThemesOfSubjectInJena(), m_conn.prefix(), escaped);
        ArrayList<String> s = m_conn.evaluateSparQLInJena(query, Queries.themeQueryName());
        return s;
    }
    
     public Theme fetch(String iri) {
         Theme result = new Theme();
         result.name = iri;
         
         String escaped = Queries.escapeValue(m_conn.prefix(), iri);
         String q;
         ArrayList<String> list;
         
         q = m_conn.buildQuery(Queries.getPropertyValues(), escaped, Queries.includedInto());
         list = this.fetchAsRow(q);
         if (list.isEmpty()) {
             result.hasParentTheme = false;
             result.parentTheme = null;
         } else {
             result.hasParentTheme = true;
             result.parentTheme = list.get(0);
         }
         
         q = m_conn.buildQuery(Queries.getPropertyValues(), escaped, Queries.relatesTo());
         list = this.fetchAsRow(q);
         if (list.isEmpty()) {
             result.subject = null;
         } else {
             result.subject = list.get(0);
         }
         
         q = m_conn.buildQuery(Queries.getPropertyValues(), escaped, Queries.educates());
         list = this.fetchAsRow(q);
         result.competences = list;
         
        
         return result;
     }
}
